package org.jeecg.modules.sync;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.materialclass.entity.MaterialClass;
import org.jeecg.modules.materialclass.mapper.MaterialClassMapper;
import org.jeecg.modules.materiel.entity.Materiel;
import org.jeecg.modules.materiel.mapper.MaterielMapper;
import org.jeecg.modules.project.entity.Project;
import org.jeecg.modules.project.mapper.ProjectMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.*;

@Slf4j
public class MatDataSync implements Job {
    final static String user = "sa";
    final static String password = "txinkj@123";
    static Connection conn = null;
    static Statement stmt = null;

    @Autowired
    private MaterialClassMapper materialClassMapper;

    @Autowired
    private MaterielMapper materielMapper;

    @Autowired
    private ProjectMapper projectMapper;

    final static String url = "jdbc:sqlserver://116.63.163.10:2198;DatabaseName=DB_XS;";
    String sql = "SELECT * FROM INDX_TO_MES WHERE STATUS =0";
    static {
        try {
            // 连接数据库
            conn = DriverManager.getConnection(url, user, password);
            // 建立Statement对象
            stmt = conn.createStatement();
        }catch (Exception ex){
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            // 连接数据库s
            conn = DriverManager.getConnection(url, user, password);
            // 建立Statement对象
            stmt = conn.createStatement();
            // 执行数据库查询语句
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                int action = rs.getInt("ACTION");
                String material_class = rs.getString("material_class");
                String material_class_code = rs.getString("material_class_code");
                if (action == 1){
                    int delete = materialClassMapper.delete(new LambdaQueryWrapper<MaterialClass>().eq(MaterialClass::getMaterialClassCode, material_class_code));
                    if (delete>0){
                        conn.createStatement().execute("UPDATE INDX_TO_MES SET STATUS = 1 WHERE material_class_code = '"+material_class_code+"'");
                        break;
                    }
                }
                String parent_id = rs.getString("parent_id");
                MaterialClass materialClass = materialClassMapper.selectOne(new LambdaQueryWrapper<MaterialClass>().eq(MaterialClass::getMaterialClassCode, parent_id));
                MaterialClass matClassInsert = new MaterialClass();
                matClassInsert.setMaterialClass(material_class);
                matClassInsert.setMaterialClassCode(material_class_code);
                matClassInsert.setParentId("0");
                if (materialClass!=null){
                    matClassInsert.setParentId(materialClass.getId());
                    MaterialClass updateParent = new MaterialClass();
                    updateParent.setId(materialClass.getId());
                    updateParent.setHasChild("1");
                    materialClassMapper.updateById(updateParent);
                }
                int updateCount = 0;
                if (action == 0){
                    updateCount = materialClassMapper.insert(matClassInsert);
                }else if (action == 2){
                    updateCount = materialClassMapper.update(matClassInsert,new LambdaQueryWrapper<MaterialClass>().eq(MaterialClass::getMaterialClassCode,material_class_code));
                }
                if (updateCount>0){
                    conn.createStatement().execute("UPDATE INDX_TO_MES SET STATUS = 1 WHERE material_class_code = '"+material_class_code+"'");
                    System.out.println("同步数据成功："+material_class_code+"\t\t\t"+material_class);
                }
            }
            ResultSet matResult = conn.createStatement().executeQuery("select * FROM PRDT_TO_MES WHERE STATUS=0");
            while (matResult.next()){
                int action = matResult.getInt("ACTION");
                String matCode = matResult.getString("mat_code");
                if (action == 1){
                    int delete = materielMapper.delete(new LambdaQueryWrapper<Materiel>().eq(Materiel::getMatCode, matCode));
                    if (delete>0){
                        conn.createStatement().execute("UPDATE PRDT_TO_MES SET STATUS = 1 WHERE mat_code = '"+matCode+"'");
                        break;
                    }
                }
                String matName = matResult.getString("mat_name");
                String matStandard = matResult.getString("mat_standard");
                float matLenght = matResult.getFloat("mat_lenght");
                float matWeight = matResult.getFloat("mat_weight");
                String matUnit = matResult.getString("mat_unit");
                String matClassId = matResult.getString("mat_class_id");
                Materiel updateMateriel = new Materiel();
                updateMateriel.setMatCode(matCode);
                updateMateriel.setMatName(matName);
                updateMateriel.setMatStandard(matStandard);
                updateMateriel.setMatLenght(Double.parseDouble(matLenght+""));
                updateMateriel.setMatWeight(Double.parseDouble(matWeight+""));
                updateMateriel.setMatUnit(matUnit);
                MaterialClass materialClass = materialClassMapper.selectOne(new LambdaQueryWrapper<MaterialClass>().eq(MaterialClass::getMaterialClassCode, matClassId));
                if (materialClass!=null && materialClass.getId() != null && (!materialClass.getId().equals(""))){
                    updateMateriel.setMatClassId(materialClass.getId());
                    updateMateriel.setClassName(matName);
                }
                int updateCount = 0;
                if (action == 0){
                    updateCount = materielMapper.insert(updateMateriel);
                }else if (action ==2){
                    updateCount = materielMapper.update(updateMateriel,new LambdaQueryWrapper<Materiel>().eq(Materiel::getMatCode,matCode));
                }

                if (updateCount>0){
                    conn.createStatement().execute("UPDATE PRDT_TO_MES SET STATUS = 1 WHERE mat_code = '"+matCode+"'");
                }

            }

            ResultSet projectResult = conn.createStatement().executeQuery("select * FROM CASN_TO_MES WHERE STATUS=0");
            while (projectResult.next()){
                int action = projectResult.getInt("ACTION");
                String projectCode = projectResult.getString("project_code");
                if (action == 1){
                    int delete = projectMapper.delete(new LambdaQueryWrapper<Project>().eq(Project::getProjectCode, projectCode));
                    if (delete>0){
                        conn.createStatement().execute("UPDATE CASN_TO_MES SET STATUS = 1 WHERE project_code = '"+projectCode+"'");
                        break;
                    }
                }
                String projectName = projectResult.getString("project_name");
                Project project = new Project();
                project.setProjectCode(projectCode);
                project.setProjectName(projectName);
                int updateCount = 0;
                if (action == 0){
                    updateCount = projectMapper.insert(project);
                }else if (action == 2){
                    updateCount = projectMapper.update(project,new LambdaQueryWrapper<Project>().eq(Project::getProjectCode,projectCode));
                }

                if (updateCount>0){
                    conn.createStatement().execute("UPDATE CASN_TO_MES SET STATUS = 1 WHERE project_code = '"+projectCode+"'");
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
    }
}
